package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.ZLContentdao;
import com.zr.model.Content;
import com.zr.model.User;
import com.zr.util.JDBCUtil;

public class ZLContentdaoimpl implements ZLContentdao {

	public List<Content> lxContentshow(int state, int rows,String lx) {
		// TODO Auto-generated method stub
		List<Content> list = new ArrayList<>();
		Connection con = JDBCUtil.getConnection();
		try {
			StringBuffer st = new StringBuffer();
			st.append("select  *   from  e_content ");
			st.append(" where cname = ? ");
			st.append(" limit ?,? ");
			PreparedStatement pst = con.prepareStatement(st.toString());
			pst.setString(1, lx);
			pst.setInt(2, state);
			pst.setInt(3, rows);
			
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Content ct = new Content();
				ct.setCid(rs.getInt("cid"));
				ct.setCname(rs.getString("cname"));
				ct.setCtitle(rs.getString("ctitle"));
				ct.setFcontent(rs.getString("ccontent"));
				ct.setCabstract(rs.getString("cabstract"));
				ct.setFnumber(rs.getInt("cnumber"));
				ct.setFspecial(rs.getString("cspecial"));
				ct.setFtime(rs.getString("ctime"));
				ct.setFstatezhuant(rs.getString("cstate"));
				list.add(ct);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public int lxContentcount( String lx) {
		// TODO Auto-generated method stub
		int count = 0;
		Connection con = JDBCUtil.getConnection();
		
       
    	   try {

   			StringBuffer st = new StringBuffer();
   			st.append("select  count(cid) as count  from  e_content where cname =? ");
   			
   			PreparedStatement pst = con.prepareStatement(st.toString());
   			pst.setString(1, lx);
   			ResultSet rs = pst.executeQuery();
   			while (rs.next()) {
   				count = rs.getInt("count");
   			}
   		} catch (SQLException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
   		
    	   return count;
       }
	

	
	
	@Override

	public List<Content> Contentshow(int state, int rows) {
		// TODO Auto-generated method stub
		List<Content> list = new ArrayList<>();
		Connection con = JDBCUtil.getConnection();
		try {
			StringBuffer st = new StringBuffer();
			st.append("select  *   from  e_content ");
			st.append(" limit ?,? ");
			PreparedStatement pst = con.prepareStatement(st.toString());
			pst.setInt(1, state);
			pst.setInt(2, rows);
			
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Content ct = new Content();
				ct.setCid(rs.getInt("cid"));
				ct.setCname(rs.getString("cname"));
				ct.setCtitle(rs.getString("ctitle"));
				ct.setFcontent(rs.getString("ccontent"));
				ct.setCabstract(rs.getString("cabstract"));
				ct.setFnumber(rs.getInt("cnumber"));
				ct.setFspecial(rs.getString("cspecial"));
				ct.setFtime(rs.getString("ctime"));
				ct.setFstatezhuant(rs.getString("cstate"));
				list.add(ct);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/*
	 * 知识类型总数
	 */
	public int Contentcount(String[] sss, String lx,int i) {
		// TODO Auto-generated method stub
		int count = 0;
		Connection con = JDBCUtil.getConnection();
		;
       if(i==0)
       {
    	   try {

   			StringBuffer st = new StringBuffer();
   			st.append("select  count(cid) as count  from  e_content ");
   			
   			PreparedStatement pst = con.prepareStatement(st.toString());
   			ResultSet rs = pst.executeQuery();
   			while (rs.next()) {
   				count = rs.getInt("count");
   			}
   		} catch (SQLException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
   		
       }
       else if(i==1)
       {
   		try {

   			StringBuffer st = new StringBuffer();
   			st.append("select  count(*)  as count    from  e_content  ");
   			st.append("where ");
   			if (lx.length() > 0) {
   				st.append("cname =?  and ");
   			}

   			st.append(" ctitle  LIKE '%");
   			st.append(sss[0]);
   			st.append("%' ");
   			for (int i1 = 1; i1 < sss.length; i1++) {
   				st.append(" and ctitle  LIKE '%");
   				st.append(sss[i1]);
   				st.append("%' ");
   			}
   			
   			PreparedStatement pst = con.prepareStatement(st.toString());
   			if (lx.length() > 0) {
   				pst.setString(1, lx);
   			}
   			
   			ResultSet rs = pst.executeQuery();
   			while (rs.next()) {
   				int num = rs.getInt("count");
   				count = num;
   			}

   		} catch (SQLException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		} 
    	   
       }
       else 
       {
    	   try {

      			StringBuffer st = new StringBuffer();
      			st.append("select  count(*)  as count    from  e_content  ");
      			st.append("where ");
      			if (lx.length() > 0) {
      				st.append("cname =?  and ");
      			}
      			st.append(" ccontent  LIKE '%");
      			st.append(sss[0]);
      			st.append("%' ");
      			for (int i1 = 1; i1 < sss.length; i1++) {
      				st.append(" and ccontent  LIKE '%");
      				st.append(sss[i1]);
      				st.append("%' ");
      			}
      			
      			PreparedStatement pst = con.prepareStatement(st.toString());
      			if (lx.length() > 0) {
      				pst.setString(1, lx);
      			}
      			
      			ResultSet rs = pst.executeQuery();
      			while (rs.next()) {
      				int num = rs.getInt("count");
      				count = num;
      			}

      		} catch (SQLException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}    
       }
       return count; 
	}

	// 通过标题查询
	public List<Content> ssbtContentshow(int state, int rows, String[] sss, String lx) {
		// TODO Auto-generated method stub
		List<Content> list = new ArrayList<>();
		Connection con = JDBCUtil.getConnection();

		try {

			StringBuffer st = new StringBuffer();
			st.append("select  *    from  e_content  ");
			st.append("where ");
			if (lx.length() > 0) {
				st.append("cname =?  and ");
			}
			if(sss.length>0)
			{
			st.append("( ctitle  LIKE '%");
			st.append(sss[0]);
			st.append("%' ");
			for (int i = 1; i < sss.length; i++) {
				st.append(" and ctitle  LIKE '%");
				st.append(sss[i]);
				st.append("%' ");
			}
			st.append(") ");
			}
			st.append("limit ?,? ");
			PreparedStatement pst = con.prepareStatement(st.toString());
			if (lx.length() > 0) {
				pst.setString(1, lx);
				pst.setInt(2, state);
				pst.setInt(3, rows);
			} else {
				pst.setInt(1, state);
				pst.setInt(2, rows);
			}
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Content ct = new Content();
				ct.setCid(rs.getInt("cid"));
				ct.setCname(rs.getString("cname"));
				ct.setCtitle(rs.getString("ctitle"));
				ct.setFcontent(rs.getString("ccontent"));
				ct.setCabstract(rs.getString("cabstract"));
				ct.setFnumber(rs.getInt("cnumber"));
				ct.setFspecial(rs.getString("cspecial"));
				ct.setFtime(rs.getString("ctime"));
				ct.setFstatezhuant(rs.getString("cstate"));
				

				list.add(ct);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	// 通过内容查询
	public List<Content> sslrContentshow(int state, int rows, String[] sss, String lx) {
		// TODO Auto-generated method stub
		List<Content> list = new ArrayList<>();
		Connection con = JDBCUtil.getConnection();

		try {

			StringBuffer st = new StringBuffer();
			st.append("select  *   from  e_content  ");
			st.append("where   ");
			if (lx.length() > 0) {
				st.append("cname =?  and ");
			}
			if(sss.length>0)
			{
			st.append("(ccontent  LIKE '%");
			st.append(sss[0]);
			st.append("%' ");
			for (int i = 1; i < sss.length; i++) {
				st.append(" and ccontent  LIKE '%");
				st.append(sss[1]);
				st.append("%' ");
			}
			st.append(") ");
			}
			st.append("limit ?,? ");
			PreparedStatement pst = con.prepareStatement(st.toString());
			if (lx.length() > 0) {
				pst.setString(1, lx);
				pst.setInt(2, state);
				pst.setInt(3, rows);
			} else {
				pst.setInt(1, state);
				pst.setInt(2, rows);
			}
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				Content ct = new Content();
				ct.setCid(rs.getInt("cid"));
				ct.setCname(rs.getString("cname"));
				ct.setCtitle(rs.getString("ctitle"));
				ct.setFcontent(rs.getString("ccontent"));
				ct.setCabstract(rs.getString("cabstract"));
				ct.setFnumber(rs.getInt("cnumber"));
				ct.setFspecial(rs.getString("cspecial"));
				ct.setFtime(rs.getString("ctime"));
				ct.setFstatezhuant(rs.getString("cstate"));
				
				list.add(ct);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

}
